package org.jeecg.common.util.valid.valid_ex;

import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraints.Pattern.Flag;
import java.util.regex.Matcher;

/**
 * 验证器  当字段不为null且长度>0  才执行正则校验
 */
@Slf4j
public class BlankOrPatternValidator implements ConstraintValidator<BlankOrPattern, String> {
    private java.util.regex.Pattern pattern;

    public BlankOrPatternValidator() {
    }

    @Override
    public void initialize(BlankOrPattern parameters) {
        Flag[] flags = parameters.flags();
        int intFlag = 0;
        Flag[] arr$ = flags;
        int len$ = flags.length;

        for (int i$ = 0; i$ < len$; ++i$) {
            Flag flag = arr$[i$];
            intFlag |= flag.getValue();
        }

        this.pattern = java.util.regex.Pattern.compile(parameters.regexp(), intFlag);
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (value == null || value.length() == 0) {
            return true;
        } else {
            Matcher m = this.pattern.matcher(value);
            return m.matches();
        }
    }
}
